package cn.cdut.yiwenqian.user.controller;

import java.io.File;
import java.util.List;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.InterceptorRefs;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

import cn.cdut.yiwenqian.biz.interfaces.IKnownleadeManager;
import cn.cdut.yiwenqian.entity.CdutPostTools;
import cn.cdut.yiwenqian.entity.CdutPostUsers;
import cn.cdut.yiwenqian.util.MyFileTool;
import cn.cdut.yiwenqian.util.MyPageToolVo;
@Namespace(value="/known")
@Controller
@InterceptorRefs(value = {@InterceptorRef("fileUploadStack")})
public class KnownleadeManagerAction extends ActionSupport {

	@Resource
	private IKnownleadeManager knownleadeBiz;
	private Logger log= Logger.getLogger(KnownleadeManagerAction.class);
	
	private String number;//主键
	private String name;//资料名称
	private String quest;//学习要求
	private String pagenum;
	private File upload;
	private String uploadFileName;
	private String uploadContentType;
	
	@Action(value="createTool", results= { @Result(name="success",location="/WEB-INF/jsp/tool/ToolInfoPage.jsp"),@Result(name="error",location="/WEB-INF/jsp/hello.jsp")})
	public String createTool() throws Exception{
		CdutPostUsers user=(CdutPostUsers) ActionContext.getContext().getSession().get("user");
		String sourcse=null;
		if(upload!=null&&uploadFileName!=null){
			sourcse=MyFileTool.saveToolSourse(upload, uploadFileName);
			log.info(sourcse);
		}else{
			ServletActionContext.getRequest().setAttribute("message", "文件上传失败！");
			return "error";
		}
		CdutPostTools tool=new CdutPostTools();
		tool.setCdutPostUsers(user);
		tool.setName(name);
		tool.setQuest(MyFileTool.createTxtFile(quest));
		tool.setSource(sourcse);
		tool=knownleadeBiz.createTool(tool);
		tool.setQuest(quest);
		ServletActionContext.getRequest().setAttribute("tool",tool);
		return "success";
	}
	@Action(value="updateTool", results= { @Result(name="success",location="/WEB-INF/jsp/tool/ToolInfoPage.jsp"),@Result(name="error",location="/WEB-INF/jsp/hello.jsp")})
	public String updateTool() throws Exception{
		String sourcse=null;
		if(upload!=null&&uploadFileName!=null){
			sourcse=MyFileTool.saveToolSourse(upload, uploadFileName);
			log.info(sourcse);
		}else{
			ServletActionContext.getRequest().setAttribute("message", "未上传文件！");
		}
		CdutPostTools tool=knownleadeBiz.findToolById(Integer.parseInt(number));
		tool.setName(name);
		MyFileTool.writeTextFile(quest, new File(tool.getQuest()));
		if(sourcse!=null){
			tool.setSource(sourcse);
		}
		tool=knownleadeBiz.updateTool(tool);
		ServletActionContext.getRequest().setAttribute("tool",tool);
		return "success";
	}
	@Action(value="findToolList", results= { @Result(name="success",location="/WEB-INF/jsp/hello.jsp")})
	public String findToolList(){
		MyPageToolVo<CdutPostTools> li=knownleadeBiz.findAllTool(Integer.parseInt(pagenum));
		ServletActionContext.getRequest().setAttribute("page", li);
		return "success";
	}
	@Action(value="findMyToolList", results= { @Result(name="success",location="/WEB-INF/jsp/user/MyKnowledage.jsp")})
	public String findMyToolList(){
		pagenum=(pagenum==null)?"1":pagenum;
		CdutPostUsers user= (CdutPostUsers) ActionContext.getContext().getSession().get("user");
		MyPageToolVo<CdutPostTools> li=knownleadeBiz.findAllUserTool(user.getUnumber(), Integer.parseInt(pagenum));
		ServletActionContext.getRequest().setAttribute("page", li);
		return "success";
	}
	@Action(value="lookupToolInfo", results= { @Result(name="success",location="/WEB-INF/jsp/tool/ToolInfoPage.jsp"),@Result(name="error",location="/WEB-INF/jsp/hello.jsp")})
	public String lookupToolInfo(){
		CdutPostTools tool=knownleadeBiz.findToolById(Integer.parseInt(number));
		if(tool==null){
			ServletActionContext.getRequest().setAttribute("message", "资料不存在！");
			return "error";
		}else{
			String fileloc=tool.getQuest();
			if(fileloc!=null){
				try {
					tool.setQuest(MyFileTool.readTxtFile(new File(fileloc)));
				} catch (Exception e) {
					ServletActionContext.getRequest().setAttribute("message", "系统错误-txt文件读取失败！"+e.toString());
					return "error";
				}
			}
			ServletActionContext.getRequest().setAttribute("tool",tool);
			return "success";
		}
	}
	
	@Action(value="toUpdateToolInfoPage", results={@Result(name="success",location="/WEB-INF/jsp/tool/UpadteToolPage.jsp"),@Result(name="error",location="/WEB-INF/jsp/hello.jsp")})
	public String toUpdateToolInfoPage(){
		CdutPostTools tool=knownleadeBiz.findToolById(Integer.parseInt(number));
		if(tool==null){
			ServletActionContext.getRequest().setAttribute("message", "资料不存在！");
			return "error";
		}else{
			String fileloc=tool.getQuest();
			if(fileloc!=null){
				try {
					tool.setQuest(MyFileTool.readTxtFile(new File(fileloc)));
				} catch (Exception e) {
					ServletActionContext.getRequest().setAttribute("message", "系统错误-txt文件读取失败！"+e.toString());
					return "error";
				}
			}
			ServletActionContext.getRequest().setAttribute("tool",tool);
			return "success";
		}
	}
	private void changeAddressToTxt(CdutPostTools tool){
		
	}
	
	/**
	 * @return the knownleadeBiz
	 */
	public IKnownleadeManager getKnownleadeBiz() {
		return knownleadeBiz;
	}
	/**
	 * @return the number
	 */
	public String getNumber() {
		return number;
	}
	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}
	/**
	 * @return the quest
	 */
	public String getQuest() {
		return quest;
	}
	/**
	 * @param knownleadeBiz the knownleadeBiz to set
	 */
	public void setKnownleadeBiz(IKnownleadeManager knownleadeBiz) {
		this.knownleadeBiz = knownleadeBiz;
	}
	/**
	 * @param number the number to set
	 */
	public void setNumber(String number) {
		this.number = number;
	}
	/**
	 * @param name the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}
	/**
	 * @param quest the quest to set
	 */
	public void setQuest(String quest) {
		this.quest = quest;
	}
	/**
	 * @return the log
	 */
	public Logger getLog() {
		return log;
	}
	/**
	 * @return the page
	 */
	public String getPagenum() {
		return pagenum;
	}
	/**
	 * @param log the log to set
	 */
	public void setLog(Logger log) {
		this.log = log;
	}
	/**
	 * @param page the page to set
	 */
	public void setPagenum(String pagenum) {
		this.pagenum = pagenum;
	}
	/**
	 * @return the upload
	 */
	public File getUpload() {
		return upload;
	}
	/**
	 * @return the uploadFileName
	 */
	public String getUploadFileName() {
		return uploadFileName;
	}
	/**
	 * @return the uploadContentType
	 */
	public String getUploadContentType() {
		return uploadContentType;
	}
	/**
	 * @param upload the upload to set
	 */
	public void setUpload(File upload) {
		this.upload = upload;
	}
	/**
	 * @param uploadFileName the uploadFileName to set
	 */
	public void setUploadFileName(String uploadFileName) {
		this.uploadFileName = uploadFileName;
	}
	/**
	 * @param uploadContentType the uploadContentType to set
	 */
	public void setUploadContentType(String uploadContentType) {
		this.uploadContentType = uploadContentType;
	}
}
